package ws.very.util.dingtalk.api

import com.dingtalk.open.client.ServiceFactory
import com.dingtalk.open.client.api.model.corp.MessageBody
import com.dingtalk.open.client.api.service.corp.ChatService

import scala.concurrent.{ExecutionContext, Future}
import collection.JavaConversions._

protected[dingtalk] trait ChatServiceHelper {
  private val serviceFactory = ServiceFactory.getInstance
  private val chatService = serviceFactory.getOpenService(classOf[ChatService])

  def accessToken: S

  //createChat(@ParamAttr(location = Location.URL,paramKey = "access_token") String var1, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "name") String var2, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "owner") String var3, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "useridlist") List<String> var4)
  def createChat(name: S, owner: S, userIdList: Seq[S])(implicit executor: ExecutionContext) = {
    Future {
      chatService.createChat(accessToken, name, owner, userIdList);
    }
  }

  //(@ParamAttr(location = Location.URL,paramKey = "access_token") String var1, @ParamAttr(location = Location.URL,paramKey = "chatid") String var2) throws ServiceException;
  def getChat(chatId: S)(implicit executor: ExecutionContext) = {
    Future {
      chatService.getChat(accessToken, chatId)
    }
  }

  //access_token") String var1, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "chatid") String var2, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "agentid
  def bindMicroapp(chatId: S, agentId: S)(implicit executor: ExecutionContext) = {
    Future {
      chatService.bindMicroapp(accessToken, chatId, agentId)
    }
  }

  //@ParamAttr(location = Location.URL,paramKey = "access_token") String var1, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "chatid") String var2, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "agentid") String var3
  def unBindMicro(chatId: S, agentId: S)(implicit executor: ExecutionContext) = Future {
    chatService.unbindMicroapp(accessToken, chatId, agentId)

  }

  //@ParamAttr(location = Location.URL,paramKey = "access_token") String var1, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "chatid") String var2, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "sender") String var3, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "msgtype") String var4, @ParamAttr(location = Location.JSON_CONTENT,paramKey = "${runtime_key}") MessageBody var5
  def sendMessageToChat(chatId: S, sender: S, msgType: S, messageBody: MessageBody)
                       (implicit executor: ExecutionContext) = Future {
    chatService.sendMessageToChat(accessToken, chatId, sender, msgType, messageBody)
  }


}
